#include "link_list.h"

Link_list* init_link_list(void) {
    Link_list *list = (Link_list *)malloc(sizeof(Link_list));
    if (list == NULL) {
        printf("创建失败\n");
        return NULL;
    }
    list->next = NULL;
    return list;
}

void free_link_list(Link_list **list) {
    if (*list == NULL) {
        printf("链表为 NULL\n");
        return;
    }

    Node *current = (*list)->next;
    Node *next;

    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }

    free(*list);
    *list = NULL;
}
void append_node(Link_list *list,Element_data data)
{
    Node *new_node=(Node *)malloc(sizeof(Node));
    if(new_node==NULL)
    {
        printf("内存分配失败\n");
        return;
    }
    new_node->data=data;
    new_node->next=NULL;

    Node *current=list->next;
    if(current==NULL)
    {
        list->next=new_node;
    }else
    {
        while(current->next !=NULL)
        {
            current=current->next;
        }
        current->next=new_node;
    }
}
void remove_last_node(Link_list *list)
{
    if(list==NULL || list->next==NULL)
    {
        printf("链表为空或者没有节点可以删除\n");
        return;
    }
    Node *current=list->next;
    Node *previous=NULL;

    while(current->next !=NULL)
    {
        previous=current;
        current=current->next;
    }
    if(previous==NULL)
    {
        free(current);
        list->next=NULL;
    }else
    {
        free(current);
        previous->next=NULL;
    }
}